﻿using Comm;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Kinlo.Model;
using Kinlo.Repo;
using Kinlo.UI.Main.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Kinlo.UI.Main.VM
{
    public class RepairInputVM:ViewModelBase
    {
        AfterWeightFirstRep DB = new AfterWeightFirstRep();
        RepairInputView View;
        public RepairInputVM(RepairInputView view)
        {
            this.View = view;
            AfterWeights = new ObservableCollection<AfterWeightFirst>();
            Code = ""; 
        }

        AfterWeightFirst currentWeight;
        public AfterWeightFirst CurW
        {
            get { return currentWeight; }
            set
            {
                currentWeight = value;
                RaisePropertyChanged(() => CurW);
            }
        }
         
        string code;
        public string Code
        {
            get { return code; }
            set
            {
                code = value;
                RaisePropertyChanged(() => Code);
            }
        }
        float? repairWeight;
        public float? RepairWeight
        {
            get { return repairWeight; }
            set
            {
                repairWeight = value;
                RaisePropertyChanged(() => RepairWeight);
            }
        }
        ObservableCollection<AfterWeightFirst> afterWeights;
        /// <summary>
        /// 补液后称重
        /// </summary>
        public ObservableCollection<AfterWeightFirst> AfterWeights
        {
            get { return afterWeights; }
            set
            {
                afterWeights = value;
                RaisePropertyChanged(() => AfterWeights);
            }
        }
        RelayCommand cmdSave;
        public RelayCommand CmdSave
        {
            get { return cmdSave ?? (cmdSave = new RelayCommand(() => DoCmdSave())); }
        }
        void DoCmdSave()
        {
            try
            {
                if (CurW != null)
                {
                    if (RepairWeight!=null&&RepairWeight > 10)
                    {
                        CurW.SupplementWeight_One = RepairWeight.Value;
                        CurW.SupplementNum_One = CurW.SupplementWeight_One - CurW.AfterWeight_One;
                        CurW.ISSuppled_One = "True";
                        CurW.ProductDate = DateTime.Now;
                        CurW.IsRead = 0;
                        var injVal = CurW.SupplementWeight_One - CurW.BeforWeight_One;
                        if (injVal >= GlobalValues.InjValMin && injVal <= GlobalValues.InjValMax)
                        {
                            CurW.InjectionNum_Offset_One = null;
                            CurW.Result_One = "True";
                        }
                        else
                        {
                            CurW.InjectionNum_Offset_One = injVal - GlobalValues.InjValStd;
                            CurW.Result_One = "False";
                        }
                        var count = DB.UpdateAfterWeightFirst(CurW);
                        if (count > 0)
                        {
                             UpdateAfterWeightsData(CurW);
                            var msg = string.Format("保存成功 {0} {1}", CurW.Code, CurW.SupplementWeight_One);
                            Logger.Info(msg);
                            MessageBox.Show(msg, "信息");
                            CurW = null; 
                            RepairWeight = null;
                            Code = "";
                        }
                        else
                        {
                            MessageBox.Show(string.Format("保存失败 {0} {1}", CurW.Code, CurW.SupplementWeight_One), "信息");
                        }

                    }
                    else
                    {
                        MessageBox.Show(string.Format("数据异常请检查 {0} {1}", CurW.Code, RepairWeight), "错误");
                    }
                }
            }
            catch (Exception ex)
            {
            }

        }
        RelayCommand cmdQuery;
        public RelayCommand CmdQuery
        {
            get { return cmdQuery ?? (cmdQuery = new RelayCommand(() => DoCmdQuery())); }
        }
        void DoCmdQuery()
        {
            try
            {
                 CurW = DB.GetAfterWeightByBarcode(Code.Trim());
                if (CurW == null)
                {
                    MessageBox.Show(string.Format("没有找到对应该的电池信息 {0}", Code.Trim()), "错误");
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        RelayCommand cmdClose;
        public RelayCommand CmdClose
        {
            get { return cmdClose ?? (cmdClose = new RelayCommand(() => DoCmdClose())); }
        }
        void DoCmdClose()
        {
            this.View.Close();
        }

        RelayCommand<RoutedEventArgs> cmdTextChanged;
        public RelayCommand<RoutedEventArgs> CmdTextChanged
        {
            get { return cmdTextChanged ?? (cmdTextChanged = new RelayCommand<RoutedEventArgs>((e) => DoCmdTextChanged(e))); }
        }
        void DoCmdTextChanged(RoutedEventArgs e)
        {
            DoCmdQuery();
        } 
        void UpdateAfterWeightsData(AfterWeightFirst w)
        {
            try
            {
                var ww = AfterWeights.Where(s => s.Code == w.Code).FirstOrDefault();
                if (ww != null)
                {
                    ww.ProductDate = w.ProductDate;
                    ww.Result_One = w.Result_One;
                    ww.ISSuppled_One = w.ISSuppled_One;
                    ww.SupplementNum_One = w.SupplementNum_One;
                    ww.SupplementWeight_One = w.SupplementWeight_One;
                    ww.InjectionNum_Offset_One = w.InjectionNum_Offset_One;
                }
                else
                {
                    AfterWeights.Add(w);
                }
                RaisePropertyChanged(() => AfterWeights);
            }
            catch (Exception ex)
            { 
            }
        }
    }
}
